use ../Data/Clean/cleaned_recruiter_data.dta, clear
drop if hiringmanagerisempty==1
egen hiring_managertypes=group(hmw hmm hme )
egen candidate_types=group(cw cm ce cb)
gen tempvar=-ppassinterview
sort screenerid tempvar, stable
by screenerid: gen rank=_n
gen t1=ppassinterview
* replace t1=rank
keep t1 screenerid candidateid
reshape wide t1, i(candidateid) j(screenerid)

set matsize 11000
unab stubs : t1*
unab stubs2 : t1*
foreach var in `stubs' {
        quietly {
                local tocorr ""
                foreach var2 in `stubs2' {
                        disp "`var' `var2'"
                        capture drop nomiss
                        gen nomiss=!missing(`var') & !missing(`var2')
                        sum nomiss if nomiss==1
                        if (r(N)>1) {
                                cor `var' `var2'
                                if (!missing(r(rho))) {
                                                local tocorr="`tocorr' `var2'"
                                        }
                        }
                }
                drop nomiss
                if (length("`tocorr'")>0) {
                        estpost corr `var' `tocorr'
                        eststo `var'
                        esttab `var' using ../Data/temp/`var'-temp-cross-mgr-correlations.csv, unstack compress b(3) nostar not nonum replace noobs mtitles() varlabels() plain
                        shell sed -i -e "s/b//g" "../Data/temp/`var'-temp-cross-mgr-correlations.csv"
                        eststo drop `var'
                        local stubs2=subinstr("`stubs2'","(`var')","",.)
                }
        }
        disp "`var'"
}

local i=0
cap erase ../Data/Temp/correlated-temp.dta
foreach var in `stubs' {
	drop _all
  capture confirm file "../Data/temp/`var'-temp-cross-mgr-correlations.csv"
  if _rc==0 {
		insheet using "../Data/temp/`var'-temp-cross-mgr-correlations.csv", comma names clear
		drop if _n==1
		if `i'>0 append using ../Data/Temp/correlated-temp-mgr.dta
		save ../Data/Temp/correlated-temp-mgr.dta, replace
		local i=1
	}
	else {
    display "The file ../Data/temp/`var'-temp-cross-mgr-correlations.csv does not exist"
  }
}

use ../Data/Temp/correlated-temp-mgr.dta, clear
rename v1 left_eid
destring t1*, replace
collapse (max) t1* , by(left_eid )

reshape long t1, i(left_eid) j(right_eid) string
replace left_eid=subinstr(left_eid, "t1", "",.)
rename t1 t1corr
drop if missing(t1corr) | left_eid==right_eid

gen pair_id=left_eid+"-"+right_eid
replace pair_id=right_eid+"-"+left_eid if right_eid>left_eid

* Dropping duplicates
bysort pair_id: gen dropme=_n
drop if dropme==2
drop dropme

sum t1corr, det
*The upper right of Table 3 is created by this command and manually entered into the table. 
